package com.doit.day03

import scala.io.Source

/**
 * 数据如下  :每个字母代表一个人 ,  统计任意一个人和其他人的共同好友
 * A:B,C,D,F,E,O
 * B:A,C,E,K
 * C:F,A,D,I
 * D:A,E,F,L
 * E:B,C,D,M,L
 * F:A,B,C,D,E,O,M
 * G:A,C,D,E,F
 * H:A,C,D,E,O
 * I:A,O
 * J:B,O
 * K:A,C,D
 * L:D,E,F
 * M:E,F,G
 * O:A,H,I,J
 */
object _08_共同好友案例 {
  def main(args: Array[String]): Unit = {
    //1.读文件
    val friends: List[String] = Source.fromFile("data/input/friends.txt").getLines().toList

    //2.处理数据  切  ： ， 放集合里面
    val myAndFriends: List[(String, Array[String])] = friends.map(line => {
      val arr: Array[String] = line.split(":")
      val my: String = arr(0)
      val friends: Array[String] = arr(1).split(",")
      (my, friends)
    })
    //3.求交集                   10
    for(elem <- 0 until myAndFriends.length -1 ){
      for(elem1 <- elem + 1 until myAndFriends.length){
        val my: (String, Array[String]) = myAndFriends(elem)
        val myFriends: Array[String] = my._2
        val you: (String, Array[String]) = myAndFriends(elem1)
        val yourFriends: Array[String] = you._2
        val sameFriends: Array[String] = myFriends.intersect(yourFriends)
        println(my._1 + "和" + you._1 + "的共同好友是："+sameFriends.toList)
      }
    }


  }

}
